let util = require('../../../utils/util.js');
let app = getApp();

Page({

  data: {
    movie: {}
  },

  onLoad: function(options) {
    let id = options.id;
    let baseurl = app.globalData.request_baseUrl;
    let url = `${baseurl}/v2/movie/subject/${id}`;
    // 请求数据
    util.http(url, this.transformData);
  },
  transformData(data) {
    if (!data)
      return;
    var director = {
      avatar: "",
      name: "",
      id: ""
    }
    if (data.directors[0] != null) {
      if (data.directors[0].avatars != null) {
        director.avatar = data.directors[0].avatars.large

      }
      director.name = data.directors[0].name;
      director.id = data.directors[0].id;
    }
    var movie = {
      movieImg: data.images ? data.images.large : "",
      country: data.countries[0],
      title: data.title,
      originalTitle: data.original_title,
      wishCount: data.wish_count,
      commentCount: data.comments_count,
      year: data.year,
      generes: data.genres.join("、"),
      stars: +data.rating.stars.slice(0, 1),
      score: data.rating.average,
      director: director,
      casts: this.convertToCastString(data.casts),
      castsInfo: this.convertToCastInfos(data.casts),
      summary: data.summary.replace(/\\n/g,'\n')
    }
    wx.setNavigationBarTitle({
      title: data.title,
    })
    this.setData({
      movie
    });
  },
  convertToCastString(casts) {
    var castsjoin = "";
    for (var idx in casts) {
      castsjoin = castsjoin + casts[idx].name + " / ";
    }
    return castsjoin.substring(0, castsjoin.length - 2);
  },
  convertToCastInfos(casts) {
    var castsArray = []
    for (var idx in casts) {
      var cast = {
        img: casts[idx].avatars ? casts[idx].avatars.large : "",
        name: casts[idx].name
      }
      castsArray.push(cast);
    }
    return castsArray;
  },
  /*查看图片*/
  viewMoviePostImg: function (e) {
    var src = e.currentTarget.dataset.src;
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: [src] // 需要预览的图片http链接列表
    })
  },
})